<?php
namespace app\plugin\controller;
use app\common\controller\Base;

class Upload extends Base {

    /**
     * base64图片上传(异步) 闭合
     * @param string $src 图片链接上传
     * @param string  返回字符串
     */
    function base64()
    {

        $params = $this->params;
        $file = $params['src'];
        header('Content-type:text/html;charset=utf-8');
        $base64_image_content = trim($file);
        //正则匹配出图片的格式
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
            $type = $result[2];//图片后缀
            $year = date('Y',time());
            $month = date('m-d', time());  //创建目录
            $new_file = ROOT_PATH . 'public'  . '/' . 'upload' . '/' . $year . '/' . $month;
            if (!file_exists($new_file)) {
                //检查是否有该文件夹，如果没有就创建，并给予最高权限
                mkdir($new_file, 0777,true);
            }
            $filename = time() . uniqid() . ".{$type}"; //文件名
            $new_file = $new_file . '/' . $filename;
            //写入操作
            if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))) {

                return '/public/upload'. '/' . $year . '/' . $month . '/' . $filename;  //返回文件名及路径
            } else {
                return false;
            }
        }
        else{
            return false;
        }
    }


    public function files(){
        $size='0,419430400';//80m
        $params = $this->params;
        if(isset($params['size'])){
            $size = $params['size'];
        }
        $size = explode(',',$size);
        if(isset($_FILES['file'])){
            $file=$_FILES['file']; //获取上传文件对象
            $name=$file['name'];
            $year = date('Y',time());
            $month = date('m-d', time());  //创建目录
            $new_file =  ROOT_PATH . 'public/upload' . '/'  . $year . '/' . $month;
            if (!file_exists($new_file)) {
                //检查是否有该文件夹，如果没有就创建，并给予最高权限
                $oldmask = umask(0);
                mkdir($new_file,0777,true);
                umask($oldmask);
            }
            $name = explode('.',$name);
            foreach($name as $k=>$v){
               $name = $v;
            }
            $filename = time() . uniqid() . ".{$name}"; //文件名
            if($file['size']<$size[1] && $file['size']>$size[0]){    //判断文件大小
                $res = move_uploaded_file($file['tmp_name'],$new_file . '/' . $filename);
                $url = 'public/upload' . '/'  . $year . '/' . $month . '/' . $filename;
                if($res){
                    $data = ['code'=>1,'msg'=>'上传成功！','data'=>['url'=>$url,'name'=>$filename,'size'=>$file['size']]];
                }
                else{
                    $data = ['code'=>0,'msg'=>'上传失败！','data'=>[]];
                }
            }else{
                $data = ['code'=>0,'msg'=>"请上传大于{$size[0]}bit小于{$size[1]}bit的文件",'data'=>['size'=>$file['size']]];
            }
        }
        else{
            $data =['code'=>0,'msg'=>'文件不存在！','data'=>[]];
        }

        $data = encode($data);
        exit($data);
    }


}